package com.yykj.hospital.server.handler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.yykj.hospital.server.dto.banci.SettingImportDTO;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@Data
public class DoctorOnlineVisitSettingHandler implements IExcelVerifyHandler<SettingImportDTO> {
    private Integer rowNum = 1;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(SettingImportDTO dto) {
        StringBuilder sb = new StringBuilder();
        boolean flag = true;
        if (StringUtils.isEmpty(dto.getJobNum())) {
            sb.append(" 工号不能为空");
            flag = false;
        }

        if(dto.getPic() < 0){
            sb.append(" 图文问诊数量不正确");
            flag = false;
        }
        if(dto.getVoice() < 0){
            sb.append(" 语音问诊数量不正确");
            flag = false;
        }
        if(dto.getVideo() < 0){
            sb.append(" 视频问诊数量不正确");
            flag = false;
        }
        if ((dto.getVoice() > 0 && dto.getVideo() > 0) || dto.getVideo() > 1 || dto.getVoice() > 1) {
            sb.append(" 视频问诊和语音问诊两者最多只有一个号源");
            flag = false;
        }

        if(!StringUtils.isEmpty(dto.getBanCiTime())){
            String[] split = dto.getBanCiTime().split("-");
            if(StringUtils.isEmpty(split[0]) || StringUtils.isEmpty(split[1])){
                sb.append(" 班次时间不正确");
                flag = false;
            }else{
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
                LocalTime startTime = LocalTime.parse(split[0],dateTimeFormatter);
                LocalTime endTime = LocalTime.parse(split[1],dateTimeFormatter);
                dto.setStartTime(startTime);
                dto.setEndTime(endTime);
                if(startTime.isAfter(endTime)){
                    sb.append(" 班次时间不正确");
                    flag = false;
                }
            }
        }


        dto.setRowNum(rowNum++);
        dto.setErrorMsg(sb.toString());

        if (flag) {
            return new ExcelVerifyHandlerResult(true, null);
        } else {
            return new ExcelVerifyHandlerResult(false, sb.toString());
        }
    }
}
